/******************************************************************************

    Copyright (C) 2006 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.engine.syspreds;

import java.util.List;
import java.util.Vector;

import prettyprolog.datatypes.Callable;
import prettyprolog.datatypes.Cons;
import prettyprolog.datatypes.Constraint;
import prettyprolog.datatypes.Nil;
import prettyprolog.datatypes.Real;
import prettyprolog.datatypes.Substitution;
import prettyprolog.datatypes.Symbol;
import prettyprolog.datatypes.Term;
import prettyprolog.datatypes.Variable;
import prettyprolog.engine.Domain;
import prettyprolog.engine.Engine;

/**
 * Implements the dom/3 system predicate.
 */
public class Labeling extends SystemPredicate {

	private static final Labeling inst = new Labeling();

	private Labeling() {
		super(1);
	}

	/** Returns the only instance of this system predicate. */
	public static Labeling getInstance() {
		return inst;
	}

	public SyspredEffect doCall(Engine en, Substitution subst, List<Term> args)	throws SyspredException {
		
		if (args.get(0) instanceof Cons) {
			Vector temp = new Vector();
			getElem((Cons) args.get(0), temp, en, subst);
//			en.getIOManager().output("Cons: "+en.getStack().top().getConstraint().getCons().toString()+"\n");
			Constraint t2 = en.getStack().top().getConstraint();
//			en.getIOManager().output("Cons: "+t2.getCons().toString()+"\n");
			Vector Adom = en.getStack().get(0).getArcDomain();
			Vector Bdom = en.getStack().get(0).getBoundDomain();
			if(!(en.getStack().top().getConstraint().isEmpty())) {
				en.Label(en.getStack().top().getConstraint(), temp, subst, Adom, Bdom);
			}
		}
//		en.SolveConstraint(en.getStack().get(0).getConstraint().getCons(), temp);
		return new SyspredEffect(false, subst);
	}
	
    public void VariableCount(List<Term> ls, Vector<Variable> V) {
//      List<Term> ls = c.getArgs();
      for(Term t : ls) {
          if (t instanceof Variable) {
              if(!V.contains((Variable) t)) {
                  V.add((Variable) ((Variable) t).getSubst());
              }
          } else if (t instanceof Callable) {
          	List<Term> args = ((Callable) t).getArgs();
              VariableCount(args, V);
          }
      }
    }
    
	public List<Term> getElem(Cons c, List<Term> temp, Engine en, Substitution sub) {
		Term tempo = c.car();
		if(tempo instanceof Real) {
//			int a = (int) ((Real) tempo).getValue();
			temp.add(tempo);
			if(c.cdr() != Nil.NIL) {
				getElem((Cons) c.cdr(), temp, en, sub);
			}
		} else if(tempo instanceof Variable) {
			temp.add(tempo);
			if(c.cdr() != Nil.NIL) {
				getElem((Cons) c.cdr(), temp, en, sub);
			}
		} else if(tempo instanceof Cons) {
			temp.add(tempo);
		}
		return temp;
	}
    
	public Vector getVar(Cons c, Vector temp) {
		Term tempo = c.car();
		temp.add(((Variable) tempo));
		if(c.cdr() != Nil.NIL) {
			getVar((Cons) c.cdr(), temp);
		}
		return temp;
	}

	public boolean isBacktrackable() {
		return false;
	}

	public String toString() {
		return super.toString() + " (dom/3)";
	}
}